package com.bloveoa.zkkd.util;

import com.bloveoa.zkkd.doman.BoxInfoModel;
import com.bloveoa.zkkd.doman.BoxInfoTokenNumModel;
import lombok.experimental.UtilityClass;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

import java.io.IOException;
import java.io.InputStream;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Stream;

/**
 * @author lihe
 */
@UtilityClass
public class StringUtils {

    /**
     * 默认的多数据分隔符
     */
    private static final String DEFAULT_MULTIPLE_DATA_SPLIT = ",";


    public static Stream<String> spiltAsStream(String data) {
        return Arrays.stream(data.split(DEFAULT_MULTIPLE_DATA_SPLIT));

    }


    public static boolean notEmpty(String str) {
        if (Objects.isNull(str)) {
            return false;
        } else {
            return !str.isEmpty();
        }
    }

    public static boolean isEmpty(String string) {
        return !notEmpty(string);
    }


    //String dateTimeStr = "2018-07-28 14:11:15";
    public static LocalDateTime getLocalDateTime(String dateTimeStr) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return LocalDateTime.parse(dateTimeStr, df);
    }

    public static double START_NUM = 10 * 10000;
    public static double START_NUM2 = 1.25 * 10000;
    public static String START_TIME = "2018-08-08 14:11:15";

    /**
     * 获取每天产出多少数量的token
     *
     * @param startTime
     * @return
     */
    public static double getNumFromYear(String startTime) {
        double num;
        Period between2 = Period.between(getLocalDateTime(startTime).toLocalDate(), LocalDateTime.now().minusDays(1).toLocalDate());
        int years = between2.getYears();
        int mouths = between2.getMonths() + years * 12;
        if (years < 3) {
            num = START_NUM / Math.pow(2, mouths / 6);
        } else {
            num = START_NUM2 / Math.pow(2, years / 4);
        }
        return num;
    }

    public static String getFileName() {
        return "DEVDATA_090233_".concat(LocalDateTime.now().minusDays(1).format(DateTimeFormatter.ofPattern("yyyyMMdd"))).concat(".txt");
        //return "DEVDATA_090233_".concat("20181128").concat(".txt");
    }

    public static BoxInfoTokenNumModel getFromFtp(List<String> datas) {
        BoxInfoTokenNumModel model = new BoxInfoTokenNumModel();
        List<BoxInfoModel> data = new ArrayList<>();
        if (datas != null && datas.size() > 0) {
            datas.forEach(s -> {
                String[] split = s.split("\\|");
                if (split.length >= 5) {
                    split = s.split("\\|", 7);
                    BoxInfoModel boxInfoModel = new BoxInfoModel();
                    boxInfoModel.setTDeviceId(split[0]);
                    int intByStr = NumberUtil.getIntByStr(split[1]);
                    int sumLong = model.getSumLong();
                    sumLong += intByStr;
                    model.setSumLong(sumLong);
                    boxInfoModel.setTOnLong(intByStr);
                    boxInfoModel.setTOpenTims(NumberUtil.getIntByStr(split[2]));
                    boxInfoModel.setTMac(split[3].toLowerCase());
                    boxInfoModel.setTBusinessId(split[4]);
                    boxInfoModel.setTBindDeviceId(split[5]);
                    boxInfoModel.setTPhone(split[6]);
                    data.add(boxInfoModel);
                }
            });
        }
        model.setDatas(data);
        model.setSumTokenNum((int) StringUtils.getNumFromYear(START_TIME));
        return model;
    }


    private void test() {

        //获取时分秒差
        Duration between = Duration.between(LocalDateTime.now(), getLocalDateTime("2014-07-28 14:11:15"));
        //年月日差
        Period between2 = Period.between(LocalDateTime.now().toLocalDate(), getLocalDateTime("2014-07-28 14:11:15").toLocalDate());

        LocalDateTime localDateTime3 = LocalDateTime.now();
        LocalDate.now();
        LocalTime.now();
        localDateTime3.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        LocalDateTime localDateTime4 = localDateTime3.minus(23, ChronoUnit.MONTHS);
        localDateTime4.atZone(ZoneId.systemDefault());
        localDateTime4 = localDateTime4.withHour(3);
        localDateTime4 = localDateTime4.withYear(2016);
        localDateTime4 = localDateTime4.with(ChronoField.MONTH_OF_YEAR, 3);

        Date date = Date.from(localDateTime4.atZone(ZoneId.systemDefault()).toInstant());

        LocalDateTime dateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());

        localDateTime3.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    public static String getMacFromData(String mac) {
        String[] split = mac.split("");
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < split.length; i++) {
            if (i % 2 == 1) {
                stringBuffer.append(split[i] + ":");
            } else {
                stringBuffer.append(split[i]);
            }
        }
        return stringBuffer.substring(0, stringBuffer.length() - 1).toLowerCase();
    }


    public static void main(String[] args) {
//        String datime = "2000-08-28 14:11:15";
        //double numFromYear = getNumFromYear(START_TIME);
        //String fileName = getFileName();
        //System.out.println(numFromYear);
//        try {
//            List<String> strings = FtpUtil.readFileByLine(getFileName());
//            System.out.println(strings);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

//        String data = "8f9169d70f4904aa|1040|1|f2:08:00:10:04:01|106642|1|";
//        String[] split = data.split("\\|", 7);
//        System.out.println(split.length);

        String data = "f20800100401";

    }
}
